<!DOCTYPE html>
<html debug="true">
  <head>
    <link rel="stylesheet" type="text/css" href="../../../../ext/resources/css/ext-all.css" />

    <script type="text/javascript" src="../../../../openlayers/lib/Firebug/firebug.js"></script>
    <script type="text/javascript" src="../../../../openlayers/lib/OpenLayers.js"></script>

    <script type="text/javascript" src="../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../ext/ext-all-debug.js"></script>

    <script type="text/javascript">
      // Because of a bug in Firefox 2 we need to specify the MapFish base path.
      // See https://bugzilla.mozilla.org/show_bug.cgi?id=351282
      var gMfLocation = "../../../../mapfish/";
    </script>
    <script type="text/javascript" src="../../../../mapfish/MapFish.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css" />

    <script type="text/javascript"><!--
        function test_constructor(t) {
            t.plan(1);
            // setup
            var grid, layer, select, mediator;
            grid = new Ext.grid.GridPanel();
            layer = new OpenLayers.Layer.Vector("vector");
            select = new OpenLayers.Control.SelectFeature(layer);
            // 1 test
            mediator = new mapfish.widgets.data.GridRowFeatureMediator({
                grid: grid,
                selectControl: select
            });
            t.eq(mediator.active, true,
                 "ctor auto-activates mediator");
        }
        function test_selection(t) {
            t.plan(6);

            // setup

            var store, grid, layer, lsm, select, feature, mediator, record;

            store = new Ext.data.Store({
                reader: new mapfish.widgets.data.FeatureReader(
                    {}, [{name: "name", type: "string"}]
                )
            });

            grid = new Ext.grid.GridPanel({
                store: store,
                renderTo: "grid",
                cm: new Ext.grid.ColumnModel([{header: "name"}])
            });

            layer = new OpenLayers.Layer.Vector("vector", {
                drawFeature: function(f, s) {
                    // do not actually draw features
                }
            });

            lsm = new mapfish.widgets.data.LayerStoreMediator({
                store: store,
                layer: layer
            });

            select = new OpenLayers.Control.SelectFeature(layer);

            feature = new OpenLayers.Feature.Vector(
                new OpenLayers.Geometry.Point(Math.random(), Math.random())
            );

            mediator = new mapfish.widgets.data.GridRowFeatureMediator({
                grid: grid,
                selectControl: select
            });

            layer.addFeatures([feature]);

            // 2 tests
            layer.events.triggerEvent("featureselected",
                {feature: feature});
            t.eq(grid.getSelectionModel().getCount(), 1,
                 "feature selection causes row selection");
            record = grid.getSelectionModel().getSelected();
            t.eq(record.id, feature.id,
                 "feature selection causes selection of expected row");

            // 1 test
            layer.events.triggerEvent("featureunselected",
                {feature: feature});
            t.eq(grid.getSelectionModel().getCount(), 0,
                 "feature unselection causes row unselection");

            // 2 tests
            grid.getSelectionModel().fireEvent("rowselect", {}, 0, record);
            t.eq(layer.selectedFeatures.length, 1,
                 "row selection causes feature selection");
            t.eq(layer.selectedFeatures[0].id, record.id,
                 "row selection causes selection of expected feature");

            // 1 test
            grid.getSelectionModel().fireEvent("rowdeselect", {}, 0, record);
            t.eq(layer.selectedFeatures.length, 0,
                 "row unselection causes feature unselection");
        }
    --></script>
  <body>
    <div id="map"></div>
    <div id="grid"></div>
  </body>
</html>
